package com.masterclient.app.modules.order.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.masterclient.app.R;
import com.masterclient.app.base.MainViewHolder;
import com.eoner.baselibrary.bean.goods.CouponMessage;
import com.eoner.baselibrary.utils.Utils;

import java.util.List;

public class CouponChooseAdapter extends DelegateAdapter.Adapter<MainViewHolder> {

    Activity mContext;
    private LayoutHelper mLayoutHelper;
    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int mCount;
    private String type;
    List<CouponMessage> list;


    public CouponChooseAdapter(Activity context, LayoutHelper layoutHelper, int count) {
        this(context, layoutHelper, count, null, null, null);
    }

    public CouponChooseAdapter(Activity context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams, List<CouponMessage> list, String type) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.mCount = count;
        this.mLayoutParams = layoutParams;
        this.list = list;
        this.type = type;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    protected void onBindViewHolderWithOffset(MainViewHolder holder, int position, int offsetTotal) {

    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    public void onBindViewHolder(MainViewHolder holder, int position) {
        if (mLayoutParams != null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        CouponMessage couponMessage = list.get(position);
        View ll_by_money = holder.itemView.findViewById(R.id.ll_by_money);
        View right_layout = holder.itemView.findViewById(R.id.right_layout);
        TextView tvMoneySign = holder.itemView.findViewById(R.id.tv_money_sign);
        TextView tvMoney = holder.itemView.findViewById(R.id.tv_money);
        TextView tvMoneyDiscountSign = holder.itemView.findViewById(R.id.tv_money_discount_sign);
        TextView tvLimit = holder.itemView.findViewById(R.id.tv_limit);
        TextView tvMoneyDes = holder.itemView.findViewById(R.id.tv_money_des);
        TextView tvCouponDate = holder.itemView.findViewById(R.id.tv_coupon_date);
        TextView tv_btn = holder.itemView.findViewById(R.id.tv_btn);
        View viewCouponDown = holder.itemView.findViewById(R.id.view_coupon_down);
        ImageView ivCouponDown = holder.itemView.findViewById(R.id.iv_coupon_down);
        TextView tvCouponMs = holder.itemView.findViewById(R.id.tv_coupon_ms);
        View viewDes = holder.itemView.findViewById(R.id.view_des);
        CheckBox checkBox = holder.itemView.findViewById(R.id.checkBox);

        tvMoney.setText(couponMessage.getDiscount_amount());
        Utils.setDin(tvMoney, mContext);
        tvLimit.setText(couponMessage.getName());
        if (!TextUtils.isEmpty(couponMessage.getDetailDesc())){
            tvMoneyDes.setVisibility(View.VISIBLE);
            tvMoneyDes.setText(couponMessage.getDetailDesc());
        }else {
            tvMoneyDes.setVisibility(View.GONE);
        }
        if ("by_fixed".equals(couponMessage.getSimple_action())) {
            tvMoneySign.setVisibility(View.VISIBLE);
            Utils.setDin(tvMoneySign, mContext);
            tvMoneySign.setText("¥");
            tvMoneyDiscountSign.setVisibility(View.GONE);
        } else {
            tvMoneySign.setVisibility(View.GONE);
            tvMoneyDiscountSign.setVisibility(View.VISIBLE);
        }
        tvCouponDate.setText(couponMessage.getFont_coupon_desc());
        tvCouponMs.setText(couponMessage.getConponDesc());

        ivCouponDown.setImageResource(R.mipmap.icon_coupon_down);
        viewCouponDown.setOnClickListener(view -> {
            if (couponMessage.isExpanded()) {
                couponMessage.setIsExpanded(false);
                ivCouponDown.setImageResource(R.mipmap.icon_coupon_down);
                tvCouponMs.setVisibility(View.GONE);
                viewDes.setVisibility(View.GONE);
                ll_by_money.setBackgroundResource(R.mipmap.bg_uesless_coupon_left);
                right_layout.setBackgroundResource(R.drawable.bg_useless_coupon_right);
            } else {
                couponMessage.setIsExpanded(true);
                ivCouponDown.setImageResource(R.mipmap.icon_coupon_up);
                tvCouponMs.setVisibility(View.VISIBLE);
                viewDes.setVisibility(View.VISIBLE);
                ll_by_money.setBackgroundResource(R.mipmap.bg_uesless_coupon_left_expanded);
                right_layout.setBackgroundResource(R.drawable.bg_useless_coupon_right_expanded);
                tvCouponMs.setBackgroundResource(R.drawable.bg_useless_coupon_down);
            }
        });

        tv_btn.setVisibility(View.GONE);
        if (type.equals("unuse")) {
            checkBox.setVisibility(View.VISIBLE);
            tvCouponDate.setTextSize(12);
        } else {
            checkBox.setVisibility(View.GONE);
            tvCouponDate.setTextSize(9);
        }
        checkBox.setChecked(couponMessage.getSelect_status());
        checkBox.setOnClickListener(view -> {
            if (onCheckChangeListerner != null) {
                onCheckChangeListerner.onCheckChange(position, !couponMessage.getSelect_status());
            }
        });

        if (couponMessage.isExpanded()) {
            ivCouponDown.setImageResource(R.mipmap.icon_coupon_up);
            tvCouponMs.setVisibility(View.VISIBLE);
            viewDes.setVisibility(View.VISIBLE);
            ll_by_money.setBackgroundResource(R.mipmap.bg_uesless_coupon_left_expanded);
            right_layout.setBackgroundResource(R.drawable.bg_useless_coupon_right_expanded);
            tvCouponMs.setBackgroundResource(R.drawable.bg_useless_coupon_down);
        } else {
            ivCouponDown.setImageResource(R.mipmap.icon_coupon_down);
            tvCouponMs.setVisibility(View.GONE);
            viewDes.setVisibility(View.GONE);
            ll_by_money.setBackgroundResource(R.mipmap.bg_uesless_coupon_left);
            right_layout.setBackgroundResource(R.drawable.bg_useless_coupon_right);
        }
    }

    public void setData(List<CouponMessage> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mCount;
    }

    private OnCheckChangeListerner onCheckChangeListerner;

    public void setOnCheckChangeListerner(OnCheckChangeListerner onCheckChangeListerner) {
        this.onCheckChangeListerner = onCheckChangeListerner;
    }

    public interface OnCheckChangeListerner {
        void onCheckChange(int position, boolean check);
    }
}
